package com.share.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.device.domain.Cabinet;
import com.share.device.domain.Station;
import com.share.device.domain.StationLocation;
import com.share.device.mapper.StationMapper;
import com.share.device.repository.StationLocationRepository;
import com.share.device.service.ICabinetService;
import com.share.device.service.IRegionService;
import com.share.device.service.IStationService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

@Service
public class StationServiceImpl extends ServiceImpl<StationMapper, Station> implements IStationService {

    @Autowired
    private ICabinetService cabinetService;

    @Autowired
    private IRegionService regionService;
    @Autowired
    private StationLocationRepository stationLocationRepository;

    @Override
    public List<Station> selectStationList(Station station) {
        List<Station> list = this.baseMapper.selectStationList(station);
        for (Station sta : list) {
            Cabinet cabinet = cabinetService.getById(sta.getCabinetId());
            sta.setCabinetNo(cabinet.getCabinetNo());
        }
        return list;
    }

    @Override
    public int saveStation(Station station) {
        String fullNameByStation = regionService.getFullNameByStation(station);
        station.setFullAddress(fullNameByStation);
        boolean save = save(station);

        StationLocation locationByStation = getStationLocationByStation(station);
        stationLocationRepository.save(locationByStation);
        return save ? 1 : 0;
    }

    @Override
    public int updateStation(Station station) {
        String fullNameByStation = regionService.getFullNameByStation(station);
        station.setFullAddress(fullNameByStation);
        boolean update = updateById(station);

//        StationLocation locationByStation = getStationLocationByStation(station);
        //        stationLocationRepository.save(locationByStation);
        StationLocation stationLocation = stationLocationRepository.getByStationId(station.getId());
        stationLocation.setLocation(new GeoJsonPoint(station.getLongitude().doubleValue(), station.getLatitude().doubleValue()));
        stationLocationRepository.save(stationLocation);
        return update ? 1 : 0;
    }


    @Override
    public Boolean dels(List<Long> list) {
        for (Object id : list) {
            stationLocationRepository.deleteByStationId(Long.parseLong(id.toString()));
        }
        return super.removeByIds(list);
    }

    private StationLocation getStationLocationByStation(Station station) {
        StationLocation stationLocation = new StationLocation();
        stationLocation.setId(station.getId().toString());
        stationLocation.setStationId(station.getId());
        stationLocation.setLocation(new GeoJsonPoint(station.getLongitude().doubleValue(), station.getLatitude().doubleValue()));
        stationLocation.setCreateTime(new Date());
        return stationLocation;
    }

    @Override
    public void updateData() {
        List<Station> stationList = this.list();
        for (Station station : stationList) {
            StationLocation stationLocation = stationLocationRepository.getByStationId(station.getId());
            if(stationLocation == null) {
                stationLocation = new StationLocation();
                stationLocation.setId(ObjectId.get().toString());
                stationLocation.setStationId(station.getId());
                stationLocation.setLocation(new GeoJsonPoint(station.getLongitude().doubleValue(), station.getLatitude().doubleValue()));
                stationLocation.setCreateTime(new Date());
                stationLocationRepository.save(stationLocation);
            }
        }
    }
}